package com.ruoyi.project.visit.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.visit.domain.DoctorPatient;
import com.ruoyi.project.visit.service.IDoctorPatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/visit/patient")
public class DoctorPatientController extends BaseController {
    @Autowired
    private IDoctorPatientService doctorPatientService;

    /**
     * 新增患者
     * @param doctorPatient
     * @return
     */
    @PostMapping
    @Log(title = "患者管理", businessType = BusinessType.INSERT)
    public AjaxResult addDoctorPatient(@RequestBody DoctorPatient doctorPatient){
        if (UserConstants.NOT_UNIQUE.equals(doctorPatientService.checkPatientCardUnique(doctorPatient))){ // 科室名称唯一
            return AjaxResult.error("新增患者'" + doctorPatient.getPatientName() + "'失败，患者身份证号已存在");
        }
        doctorPatient.setCreateBy(SecurityUtils.getUsername()); // 设置登录用户名
        // 新增患者
        int rows = doctorPatientService.insertDoctorPatient(doctorPatient);
        if(rows == 0){
            // 新增失败
            return AjaxResult.error();
        }
        // 新增成功，把新增的患者对象返回，需要其中的主键
        return AjaxResult.success(doctorPatient);
    }

    /**
     * 根据身份证号，查询患者
     * 未提供权限相关的配置（菜单管理中的配置）。省略权限校验。
     * @param patientCode
     * @return
     */
    @GetMapping("/patient/{patientCode}")
    public AjaxResult getPatientByCode(@PathVariable String patientCode){
        return AjaxResult.success(doctorPatientService.selectByCode(patientCode));
    }
}
